< previous page page_402 next page >

Page 402
In the Write command button, you begin by dimming a variable named oFile as a TextStream class. This instantiates a new object based on the TextStream class. You can now use the properties and methods of this object to write the data to the text file. Heres the code:
9f31265bb40b592b1929e18a47ab28e5.gif
Private Sub cmdWrite_Click()
   Dim oFile As TextStream
   Dim boolRet As Boolean

   Set oFile = New TextStream
9f31265bb40b592b1929e18a47ab28e5.gif
   With oFile
      If .OpenTextFile(txtFile.Text, ForAppending) Then
         Call .WriteLine(txtEdit)
      Else
         MsgBox "Error opening text file"
      End If

      .CloseFile
   End With
   Set oFile = Nothing
End Sub
The OpenTextFile() method is passed in the name of the file and the mode in which to open the file. If the file does not exist, it will be created. If theres an error in creating the file, a False value will be returned from the method, and you can then perform appropriate error handling. After the file is open, call the WriteLine() method, passing in the text that you want to store in the file.
Lets take a look at the OpenTextFile() method to see how it works:
9f31265bb40b592b1929e18a47ab28e5.gif
Public Function OpenTextFile(strFile As String, _
        Optional intOpenMode As TSOpenMode = ForReading, _
        Optional intFormat As TSFormat = TristateTrue) _
            As Boolean
   If strFile <> "" Then
      mstrFileName = strFile
      mboolEOF = False
      mintOpenMode = intOpenMode
      mintFormat = intFormat

      If intOpenMode = ForReading Then

 
< previous page page_402 next page >

If you like this book, buy it!